package com.pet.server.bo.common;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.pet.framework.page.Page;
import com.pet.framework.util.StringUtil;
import com.pet.server.dao.common.ArticleDao;
import com.pet.server.po.Article;
import com.pet.server.po.ArticleTemp;
import com.pet.server.po.Subject;
import com.pet.system.constants.ATTSystem;
import com.pet.system.constants.BaseAttribute;
import com.pet.system.util.FolderManager;

public class ArticleService extends CommonService {

	private ArticleDao articleDao;
	public void saveArticle(Article article,File image1,File image2,File image3,String image1FileName,String image2FileName,String image3FileName){
		String path1 = FolderManager.uploadFile(image1,image1FileName);
		String path2 = FolderManager.uploadFile(image2,image2FileName);
		String path3 = FolderManager.uploadFile(image3,image3FileName);
		article.setImage1(path1);
		article.setImage2(path2);
		article.setImage3(path3);
		
		article.setCreateTime(new Date());
		article.setState(ATTSystem.LOGIC_TRUE.equals(article.getState())?ATTSystem.LOGIC_TRUE:ATTSystem.LOGIC_FALSE);
		saveObject(article);
	}
	
	
	public void saveArticle(Article article,ArticleTemp temp,File image1,File image2,File image3,String image1FileName,String image2FileName,String image3FileName){
		String path1 = FolderManager.uploadFile(image1,image1FileName);
		String path2 = FolderManager.uploadFile(image2,image2FileName);
		String path3 = FolderManager.uploadFile(image3,image3FileName);
		article.setImage1(StringUtil.dealNull(path1, temp.getImage1()));
		article.setImage2(StringUtil.dealNull(path2, temp.getImage2()));
		article.setImage3(StringUtil.dealNull(path3, temp.getImage3()));
		
		
		
		article.setCreateTime(new Date());
		article.setState(ATTSystem.LOGIC_TRUE.equals(article.getState())?ATTSystem.LOGIC_TRUE:ATTSystem.LOGIC_FALSE);
		saveObject(article);
	}
	
	
	public void updateArticle(Article article,Article nale,File image1,File image2,File image3,String image1FileName,String image2FileName,String image3FileName){
		String path1 = FolderManager.uploadFile(image1,image1FileName);
		String path2 = FolderManager.uploadFile(image2,image2FileName);
		String path3 = FolderManager.uploadFile(image3,image3FileName);
		if(!StringUtil.isEmpty(path1)){
			article.setImage1(path1);
		}
		if(!StringUtil.isEmpty(path2)){
			article.setImage2(path2);
		}
		if(!StringUtil.isEmpty(path3)){
			article.setImage3(path3);
		}
		article.setPetCate(nale.getPetCate());
		article.setAgeCate(nale.getAgeCate());
		article.setViewType(nale.getViewType());
		article.setTitle(nale.getTitle());
		article.setDescription(nale.getDescription());
		article.setText(nale.getText());
		article.setCate(nale.getCate());
		article.setPetBrand(nale.getPetBrand());
		article.setSubjectId(nale.getSubjectId());
		article.setState(ATTSystem.LOGIC_TRUE.equals(nale.getState())?ATTSystem.LOGIC_TRUE:ATTSystem.LOGIC_FALSE);
        updateObject(article);
	}
	public void searchArticle(Page page,Article article){
		articleDao.searchArticle(page, article);
	}
	
	public void searchArticleTemp(Page page,ArticleTemp article){
		articleDao.searchArticleTemp(page, article);
	}
	
	public ArticleDao getArticleDao() {
		return articleDao;
	}
	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}
	public void searchSubject(Page page,Subject subject){
		articleDao.searchSubject(page, subject);
	}
	public void saveSubject(Subject subject,File ico,String icoFileName){
		if(subject!=null){
			String path = FolderManager.uploadFile(ico,icoFileName);
			subject.setIcoPath(path);
			subject.setCreatedTime(new Date());
			subject.setState(ATTSystem.LOGIC_TRUE.equals(subject.getState())?ATTSystem.LOGIC_TRUE:ATTSystem.LOGIC_FALSE);
            saveObject(subject);				
		}
	}
	public void updateSubject(Subject subject,Subject osj,File ico,String icoFileName){
		if(subject!=null){
			String path = FolderManager.uploadFile(ico,icoFileName);
			if(!StringUtil.isEmpty(path)){
				osj.setIcoPath(path);	
			}
			osj.setDescription(subject.getDescription());
			osj.setTitle(subject.getTitle());
			osj.setState(subject.getState());
			osj.setState(ATTSystem.LOGIC_TRUE.equals(subject.getState())?ATTSystem.LOGIC_TRUE:ATTSystem.LOGIC_FALSE);
            updateObject(osj);				
		}
	}
	public List<Subject> querySubject(){
		return articleDao.querySubject();
	}
	
	
	
}
